package com.hw.one.peach.core.controller;

import com.hw.one.core.controller.BaseController;
import com.hw.one.core.result.MsgResult;
import com.hw.one.ext.util.UserUtil;
import com.hw.one.peach.core.service.ISiteDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/4.
 */
@RestController
@RequestMapping(path = "siteData")
public class SiteDataController extends BaseController{


    @Autowired
    private ISiteDataService siteDataService;
    /**
     * 根据点位编码，查询单个点位详细信息 比如，空气站最近两个小时的5分钟数据等
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/findSiteLastDetailBySiteCode", method = RequestMethod.GET)
    public MsgResult findSiteLastDetailBySiteCodeForTrend(HttpServletRequest request, HttpServletResponse response) {
        // 点位编码
        String siteCode = request.getParameter("siteCode");
        // siteCode = "125";
        // 点位类型
        String siteType = request.getParameter("siteType");
        // 默认国控点nationalSite（省控点 provinceSite 小型站 smallAirSite 工业源 industrySite）
        siteType = null == siteType || "".equals(siteType) ? "nationalSite" : siteType;

        if (null == siteCode || "".equals(siteCode)) {
            return MsgResult.infoResult("查询条件点位编码siteCode为空，查询失败。");
        }
        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("siteType", siteType);
        paraMap.put("siteCode", siteCode);

        try {
            Map<String, Object> datailList = siteDataService.findAirSiteLastDetailListBySiteTypeForTrend(paraMap);
            return MsgResult.okResult(datailList);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }

    }


    /**
     * 查询框选区域范围内的站点
     * @author 李亚坤
     * @createtime 2017年8月24日18:00:47
     * @param request
     * @return
     */
    @RequestMapping(value = "/findSiteDataBySelectBox", method = RequestMethod.GET)
    public MsgResult findSiteDataBySelectBox(HttpServletRequest request) {
        try {
            // 获取参数
            Map<String, Object> paramMap = wrapParams(request);

            String pointTypes = (String) paramMap.get("pointTypes");
            String lngSets = (String) paramMap.get("lngSets");
            String latSets = (String) paramMap.get("latSets");
            Map<String, Object> region = UserUtil.getRegion();
            String userRegionCode = (String) region.get("regionCode");   //当前用户所在的区域

            if (StringUtils.isBlank(lngSets) || StringUtils.isBlank(latSets)) {
                return MsgResult.nullResult("框选区域经纬度");
            }
            paramMap.put("regionCode",  userRegionCode);
            Map<String, Object> resultMap = siteDataService.findSiteDataBySelectBox(paramMap);
            return MsgResult.okResult(resultMap);
        }catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }
    }

}
